/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prabhu.jeazyprops.bean;

import com.prabhu.jeazyprops.props.BaseProps;
import com.prabhu.jeazyprops.props.Constants;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * File Chooser Component Class
 *
 * @author Prabhu Prabhakaran
 */
public class FilePanel extends JPanel {

    JTextField jTextField;
    JButton jButton;
    JFileChooser jFileChooser;
    boolean lEncrypt;

    /**
     * Creates File chooser Panel
     *
     * @param pEncrypt
     */
    public FilePanel(boolean pEncrypt) {
        lEncrypt = pEncrypt;
        if (pEncrypt) {
            jTextField = new JPasswordField();
        } else {
            jTextField = new JTextField();
        }
        jButton = new JButton(Constants.get(Constants.FileBrowseButtonString));
        jFileChooser = new JFileChooser();
        jTextField.setEditable(false);
        setLayout(new GridBagLayout());
        GridBagConstraints lBagConstraints = new GridBagConstraints();
        lBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        lBagConstraints.gridx = 0;
        lBagConstraints.gridy = 0;
        lBagConstraints.insets = new Insets(0, 0, 0, 0);
        lBagConstraints.weightx = 1.0;
        if (pEncrypt) {
            add((JPasswordField) jTextField, lBagConstraints);
        } else {
            add(jTextField, lBagConstraints);
        }
        lBagConstraints.insets = new Insets(0, 5, 0, 0);
        lBagConstraints.weightx = 0.01;
        lBagConstraints.gridx = 1;
        add(jButton, lBagConstraints);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                int returnVal = jFileChooser.showOpenDialog(jButton.getParent());
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                        File file = jFileChooser.getSelectedFile();
                        jTextField.setText(file.getCanonicalPath());
                    } catch (IOException ex) {
                        Logger.getLogger(BaseProps.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    }

    /**
     * Sets File name to the TextBox
     *
     * @param pFileName
     */
    public void setFileName(String pFileName) {
        jTextField.setText(pFileName);
    }

    /**
     * Gets File name from TextBox
     *
     * @return
     */
    public String getFileName() {
        return jTextField.getText();
    }

    public JComponent getActionComponent() {
        return jButton;
    }
}
